package io.zhifei.modules.sys.system.service.member.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.R;
import io.zhifei.common.utils.ToolUtils;
import io.zhifei.modules.sys.system.dao.member.SysMemberFollowDao;
import io.zhifei.modules.sys.system.entity.member.SysMemberFollowEntity;
import io.zhifei.modules.sys.system.form.member.SysMemberFollowParam;
import io.zhifei.modules.sys.system.form.member.SysMemberFollowResult;
import io.zhifei.modules.sys.system.service.member.SysMemberFollowService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 学员跟进 服务实现类
 * </p>
 *
 * @author duke
 * @since 2020-12-21
 */
@Service
public class SysMemberFollowServiceImpl extends ServiceImpl<SysMemberFollowDao, SysMemberFollowEntity> implements SysMemberFollowService {

    @Override
    public void add(SysMemberFollowParam param) {
        SysMemberFollowEntity entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(SysMemberFollowParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(SysMemberFollowParam param) {
        SysMemberFollowEntity oldEntity = getOldEntity(param);
        SysMemberFollowEntity newEntity = getEntity(param);
        ToolUtils.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public PageUtils getPageList(Page page, SysMemberFollowParam param) {
        IPage pageList = this.baseMapper.getPageList(page,param);
        return new PageUtils(pageList);
    }

    @Override
    public R getTotalList(String time) {
        Map<String,Object> result = new HashMap<>();
        Date date = new Date();
        if (StrUtil.isNotEmpty(time)){
            date = DateUtil.parseDate(time);
        }
        //获取时间对应的周一时间
        String beginDay = DateUtil.beginOfWeek(date).toString();
        //获取统计列表
        List<Map<String,Object>> totalList = this.baseMapper.getTotalList(beginDay);
        //获取统计的总计数据
        Map<String,Object> totalSum = this.baseMapper.getTotalSum(beginDay);
        totalList.add(totalSum);
        //获取统计展示的日期信息
        Map<String,Object> totalDate = this.baseMapper.getTotalDate(beginDay);
        result.put("list",totalList);
        result.put("date",totalDate);
        return R.ok().put("data",result);
    }

    private Serializable getKey(SysMemberFollowParam param) {
        return param.getId();
    }


    private SysMemberFollowEntity getOldEntity(SysMemberFollowParam param) {
        return this.getById(getKey(param));
    }

    private SysMemberFollowEntity getEntity(SysMemberFollowParam param) {
        SysMemberFollowEntity entity = new SysMemberFollowEntity();
        ToolUtils.copyProperties(param, entity);
        return entity;
    }

}
